const { QueryTypes } = require('sequelize');

const querystring = require('querystring');
const count = 5; // 按下载量排序时，默认返回5条

module.exports = {
    getCurrentFolder: async (ctx) => {
        let body;
        try {
            body = ctx.sequelize.query(
                `SELECT * from design_folders order by orders;`,
                {
                    timestamps: false,
                    plain: false,
                    raw: false,
                    type: QueryTypes.SELECT,
                }
            );
        } catch (error) {
            body = '';
        }
        return body;
    },
    addOneFolder: async (ctx) => {
        // console.log('ctx', ctx);
        const queryObj = querystring.parse(ctx.url.split('?')[1]);
        const folder = queryObj.folder || '';
        const order = queryObj.order || '';
        if (folder === '' || order === '') {
            body = '';
            return body;
        }
        let body;
        try {
            body = ctx.sequelize.query(`INSERT INTO design_folders(name, orders) VALUES ('${folder}', ${order})`,
                {timestamps: false, plain: false, raw: false, type: QueryTypes.INSERT,});
        } catch (error) {
            body = '';
        }
        return body;
    },
    changeOneOrder: async (ctx) => {
        const queryObj = querystring.parse(ctx.url.split('?')[1]);
        const folder = queryObj.folder || '';
        const order = queryObj.order || '';
        if (folder === '' || order === '') {
            body = '';
            return body;
        }
        let body;
        try {
            body = ctx.sequelize.query(`UPDATE design_folders SET orders = ${order} WHERE name = '${folder}'`,
                {timestamps: false, plain: false, raw: false, type: QueryTypes.UPDATE,});
        } catch (error) {
            body = '';
        }
        return body;
    },
    deleteOneFolder: async (ctx) => {
        // console.log('ctx', ctx);
        const queryObj = querystring.parse(ctx.url.split('?')[1]);
        const folder = queryObj.folder || '';
        if (folder === '') {
            body = '';
            return body;
        }
        let body;
        try {
            body = ctx.sequelize.query(`DELETE FROM design_folders WHERE name = '${folder}'`,
                {timestamps: false, plain: false, raw: false, type: QueryTypes.DELETE,});
        } catch (error) {
            body = '删除失败';
        }
        return body;
    },
    deleteOneFolderFiles: async (ctx) => {
        // console.log('ctx', ctx);
        const queryObj = querystring.parse(ctx.url.split('?')[1]);
        const folder = queryObj.folder || '';
        if (folder === '') {
            body = '';
            return body;
        }
        let body;
        try {
            body = ctx.sequelize.query(`UPDATE design_records SET is_delete = 1 WHERE folder = '${folder}'`,
                {timestamps: false, plain: false, raw: false, type: QueryTypes.UPDATE,});
        } catch (error) {
            body = '';
        }
        return body;
    },
    renameOneFolder: async (ctx) => {
        // console.log('ctx', ctx);
        const queryObj = querystring.parse(ctx.url.split('?')[1]);
        const folder = queryObj.folder || '';
        const newName = queryObj.new || '';
        if (folder === '') {
            body = '';
            return body;
        }
        let body;
        try {
            body = ctx.sequelize.query(`UPDATE design_folders SET name = '${newName}' WHERE name = '${folder}'`,
                {timestamps: false, plain: false, raw: false, type: QueryTypes.UPDATE,});
        } catch (error) {
            body = '';
        }
        return body;
    },
    renameOneFolderFiles: async (ctx) => {
        // console.log('ctx', ctx);
        const queryObj = querystring.parse(ctx.url.split('?')[1]);
        const folder = queryObj.folder || '';
        const newName = queryObj.new || '';
        if (folder === '') {
            body = '';
            return body;
        }
        let body;
        try {
            const folderReplace = folder + '\\';
            const newNameReplace = newName + '\\';
            body = ctx.sequelize.query(`UPDATE design_records SET full_path = replace(full_path, '${folderReplace}', '${newNameReplace}'), 
            relative_path = replace(relative_path, '${folderReplace}', '${newNameReplace}'), folder = '${newName}' WHERE folder = '${folder}'`,
                {timestamps: false, plain: false, raw: false, type: QueryTypes.UPDATE,});
            // body = ctx.sequelize.query(`UPDATE design_records SET folder = '${newName}' WHERE name = '${folder}'`,
            //     {timestamps: false, plain: false, raw: false, type: QueryTypes.UPDATE,});
        } catch (error) {
            body = '';
        }
        return body;
    },
};
